const http = require('../../../utils/http.js'); 
Page({
  /**
   * 页面的初始数据
   */
  data: {
    testdata: '',
    userid: '',
    sexchecked: true,
    list: ['白', '黑', '红', '灰', '棕', '蓝','黄','粉','紫','绿','其他'],
    list1: ['京','沪','津','渝','鲁','冀','晋','蒙','辽','吉','黑','苏','浙','皖','闽','赣','豫','湘','鄂','粤','桂','琼','川','贵','云','藏','陕','甘','青','宁','新','港','澳','台'],
    index: '',
    index1: '',
    baseData: '',
    imgPath: 'https://mocar.img.xzozo.com/img/icon/home/',
    show: false,
    chepaiqianzui: false,
    from: {
      chepai: '',
      vin: '',
      chexin: '',
      factory_price: '',
      nianfen: '',
      chelin: '',
      fadongjihao: '',
      cheshencolor: '',
      jiedaiguwen: '',
      jiandangriqi: '',
      jiandangmendian: '',
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let from = this.data.from;
    from.car_prefix = options.carpai;
    from.car_number = options.carnum;
    this.setData({
      from: from
    })
  },
  onShow(){
    this.getbase_data();
    if(this.data.testdata.id && this.data.testdata.type == 'series') {
      http.postRequest('/mini/v1/car_owner/brand_by_series',{seriesId: this.data.testdata.id},res=> {
        let from = this.data.from;
        from.chexin = res.data.brand_data.brandName + res.data.series_data.seriesName;
        from.brand_id = res.data.brand_data.id;
        from.series_id = res.data.series_data.id;
        this.setData({
          from: from
        })
      })
    } else if(this.data.testdata.id && this.data.testdata.type == 'type') {
      http.postRequest('/mini/v1/car_owner/brand_series_by_type',{typeId: this.data.testdata.id},res=> {
        let from = this.data.from;
        from.chexin = res.data.brand_data.brandName + res.data.series_data.seriesName + res.data.type_data.typeName;
        from.brand_id = res.data.brand_data.id;
        from.series_id = res.data.series_data.id;
        from.type_id = res.data.type_data.id;
        this.setData({
          from: from
        })
      })
    }
    if(this.data.userid) {
      http.postRequest('/mini/v1/car_owner/owner_info',{owner_id: this.data.userid},res=> {
        let from = this.data.from;
        from.name = res.data.name;
        from.mobile = res.data.mobile;
        from.owner_id = this.data.userid;
        this.setData({
          from: from
        })
      })
    }
  },
   // 获取页面基础数据
   getbase_data() {
    http.postRequest('/mini/v1/car_owner/base_data',{},res=> {
      this.setData({
        baseData: res.data
      })
    })
  },
  qinkon() {
    let from = this.data.from
    const that = this
    wx.showModal({
      content: '您确定要清除所有信息吗',
      success: function (res) {
        if (res.confirm) {
          for (let i in from) {
            from[i] = ''
          }
          that.setData({
            from: from
          })
        }
      }
    })

  },
  packl() {
    wx.navigateTo({
      url: '/pages/Cartype/Cartype',
    })
  },
  packl2() {
    let list = this.data.baseData.staff;
    let from = this.data.from
    const that = this
    wx.showActionSheet({
      itemList: list,
      success: function (res) {
        from.jiedaiguwen = list[res.tapIndex]
        that.setData({
          from: from
        })
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  fromqianzui() {
    this.setData({
      fromqianzui: true
    })
  },
  qianzuiOk(e){
    let text = e.currentTarget.dataset.text;
    let from = this.data.from;
    from.car_prefix = text;
    this.setData({
      from: from,
      fromqianzui: false
    })
  },
  bind() {
    wx.navigateTo({
      url: "/pages/classify/binduser"
    })
  },
  show() {
    let show = this.data.show
    show = !show
    this.setData({
      show: show
    })
  },
  radioChange(e) {
    let from = this.data.from;
    from.gender = e.detail.value == '男' ? 0 : 1;
    this.setData({
      from: from
    })
  },
  // 确定新建
  gopage(){
    let from = this.data.from;
    if(from.car_prefix && from.car_number && from.name && from.mobile && from.staff_id) {
      if(!from.gender && from.gender != 0) {
        from.gender = 0;
      }
      http.postRequest('/mini/v1/car_owner/add_all',from,res=> {
        if(res.code == 0) {
          wx.navigateBack({delta: -2});
        }
        if(res.code == 2003) {
          wx.showToast({
            title: '车辆已存在',
            icon: 'none'
          })
        }
      })
    } else {
      wx.showToast({
        title: '必填项不能为空',
        icon: 'none'
      })
    }
  },
  // 车身颜色
  bindPickerChange(e){
    let from = this.data.from;
    from.color = this.data.list[e.detail.value];
    this.setData({
      index: e.detail.value,
      from: from
    })
  },
  // 接待顾问
  bindPickerChange1(e){
    let from = this.data.from;
    from.staff_id = this.data.baseData.staff[e.detail.value].id;
    this.setData({
      index1: e.detail.value,
      from: from
    })
  },
  // 双向数据流
  fromChepai(e) {
    let from = this.data.from;
    from.car_number = e.detail.value;
    this.setData({
      from: from
    })
  },
  fromChepai1(e){
    let from = this.data.from;
    from.vin = e.detail.value;
    this.setData({
      from: from
    })
  },
  fromChepai2(e){
    let from = this.data.from;
    from.chexin = e.detail.value;
    this.setData({
      from: from
    })
  },
  fromChepai3(e){
    let from = this.data.from;
    from.factory_price = e.detail.value;
    this.setData({
      from: from
    })
  },
  fromChepai4(e){
    let from = this.data.from;
    from.nianfen = e.detail.value;
    this.setData({
      from: from
    })
  },
  fromChepai5(e){
    let from = this.data.from;
    from.chelin = e.detail.value;
    this.setData({
      from: from
    })
  },
  fromChepai6(e){
    let from = this.data.from;
    from.engine_number = e.detail.value;
    this.setData({
      from: from
    })
  },
  infoIpt(e){
    let from = this.data.from;
    from.name = e.detail.value
    this.setData({
      from: from
    })
  },
  infoIpt1(e) {
    let from = this.data.from;
    from.remark = e.detail.value
    this.setData({
      from: from
    })
  },
  infoIpt2(e) {
    let from = this.data.from;
    from.mobile = e.detail.value
    this.setData({
      from: from
    })
  }
})